package com.shanghaionstar.notifysetting;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.annotation.ContentView;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.notify.NotifyConfigRes;
import com.onstar.cn.smart.BaseResponse;
import com.onstar.cn.weight.ProgressLayout;
import com.onstar.cn.weight.more.CustomLineLayout;
import com.shanghaionstar.R;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.notifysetting.presenter.NotifyPresenter;
import com.shanghaionstar.swipe.SwipeActivity;
import com.shanghaionstar.utils.AlertDialogUtil;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.weight.TopLayout;

/**
 * 车辆故障诊断提醒渠道设置
 * Created by Yonglang Hong on 2017/3/17.
 */
@ContentView(R.layout.activity_setnotify_common)
public class CommonNotifySetActivity extends SwipeActivity implements IMVPView {
    private NotifyPresenter mPresenter;
    private static final int CHECK_SMS = 100;
    private static final int CHECK_APP = 101;


    @FindViewById
    private CustomLineLayout cll_setNotifyApp, cll_setNotifySms;

    @FindViewById
    private TextView tv_tipNotifyApp, tv_tipNotifySms;


    @FindViewById
    private TopLayout top_layout;

    @FindViewById
    private ProgressLayout progress_layout;

    private NotifyConfigRes mNotifyConfig;

    private int whichCheck;
    private boolean isPreSelected;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter = new NotifyPresenter(getScheduler());
        mPresenter.setView(this);
        initView();
    }

    private String notifyType;

    private void initView() {
        top_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BusinessUtil.sendReportByClickType(getApplication(), FunctionNameFindId.DASetting_ClickBack.getStringValue());
                finish();
            }
        });
        Intent it = getIntent();
        if (it != null) {
            mNotifyConfig = (NotifyConfigRes) it.getSerializableExtra(NotifyPresenter.IBType.TYPE_COMMON);
            notifyType = it.getStringExtra("type");
        }
        if (mNotifyConfig != null) {
            SmsEmptyHelper helper = new SmsEmptyHelper(this, mNotifyConfig);
            String smsChecked = mNotifyConfig.getSms_checked();
            boolean notifySms = !TextUtils.isEmpty(smsChecked) && smsChecked.equalsIgnoreCase("1");
            cll_setNotifySms.setSelected(notifySms);

            String notifySmsTip = getString(R.string.notify_common_phone);
            String typeTip = "";

            if (notifyType.equalsIgnoreCase(NotifyPresenter.IBType.TYPE_DS)) {
                typeTip = getString(R.string.notify_ds_tip);
                top_layout.setTopTitle(getString(R.string.set_notify_DS));
            } else if (notifyType.equalsIgnoreCase(NotifyPresenter.IBType.TYPE_OL)) {
                typeTip = getString(R.string.notify_ol_tip);
                top_layout.setTopTitle(getString(R.string.set_notify_OL));
            } else if (notifyType.equalsIgnoreCase(NotifyPresenter.IBType.TYPE_VI)) {
                typeTip = getString(R.string.notify_ubi_tip);
                top_layout.setTopTitle(getString(R.string.set_notify_VI));
            }


            notifySmsTip = String.format(notifySmsTip, typeTip, CommonUtil.replaceString(mNotifyConfig.getPhone(), false));
            helper.handleSms(tv_tipNotifySms, notifySmsTip);
//            tv_tipNotifySms.setText(notifySmsTip);

            tv_tipNotifySms.setVisibility(notifySms ? View.VISIBLE : View.GONE);


//            tv_tipNotifySms, tv_tipNotifyApp, tv_tipNotifyPhone
            String mobileChecked = mNotifyConfig.getMobile_checked();
            boolean notifyApp = !TextUtils.isEmpty(mobileChecked) && mobileChecked.equalsIgnoreCase("1");
            cll_setNotifyApp.setSelected(notifyApp);
            tv_tipNotifyApp.setText(String.format(getString(R.string.notify_common_app), typeTip));
            tv_tipNotifyApp.setVisibility(notifyApp ? View.VISIBLE : View.GONE);
//            cll_setNotifyApp.setSwitchVisable(false);

        }
        cll_setNotifySms.setSwitchCallback(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (notifyType != null && notifyType.equalsIgnoreCase(NotifyPresenter.IBType.TYPE_OL)) {
                    BusinessUtil.sendReportByClickType(getApplication(), buttonView.isSelected() ? FunctionNameFindId.FuelEconomySetting_SMSOFF.getStringValue() : FunctionNameFindId.FuelEconomySetting_SMSON.getStringValue());
                }
                onRealCheckedChanged(buttonView, CHECK_SMS);
            }
        });
        cll_setNotifyApp.setSwitchCallback(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (notifyType != null && notifyType.equalsIgnoreCase(NotifyPresenter.IBType.TYPE_OL)) {
                    BusinessUtil.sendReportByClickType(getApplication(), buttonView.isSelected() ? FunctionNameFindId.FuelEconomySetting_APPNotificationOFF.getStringValue() : FunctionNameFindId.FuelEconomySetting_APPNotificationON.getStringValue());
                }
                onRealCheckedChanged(buttonView, CHECK_APP);
            }
        });

    }

    private void onRealCheckedChanged(CompoundButton buttonView, int wh) {
        whichCheck = wh;
        isPreSelected = buttonView.isSelected();
        buttonView.setSelected(!isPreSelected);
        switch (wh) {
            case CHECK_SMS:
                mNotifyConfig.setSms_checked(!isPreSelected ? "1" : "0");
                break;
            case CHECK_APP:
                mNotifyConfig.setMobile_checked(!isPreSelected ? "1" : "0");
                break;
        }
        mPresenter.setNotifyConfig(mNotifyConfig);
    }


    @Override
    public BasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    public void setLoading(boolean isLoading) {
        progress_layout.setVisibility(isLoading ? View.VISIBLE : View.GONE);
    }

    @Override
    public void onResponseError(OnStarException e) {
        AlertDialogUtil.showDialogByOnstarException(onActivity(), e);
        onSetConfigFailed();
    }

    private void onSetConfigFailed() {
        switch (whichCheck) {
            case CHECK_SMS:
                cll_setNotifySms.setSelected(isPreSelected);
                mNotifyConfig.setSms_checked(isPreSelected ? "1" : "0");
                tv_tipNotifySms.setVisibility(isPreSelected ? View.VISIBLE : View.GONE);
                break;
            case CHECK_APP:
                cll_setNotifyApp.setSelected(isPreSelected);
                mNotifyConfig.setMobile_checked(isPreSelected ? "1" : "0");
                tv_tipNotifyApp.setVisibility(isPreSelected ? View.VISIBLE : View.GONE);
                break;
        }
    }

    @Override
    public void onReponseSuccess(Object object) {
        if (object != null && object instanceof BaseResponse) {
            BaseResponse response = (BaseResponse) object;
            if (!response.code.equalsIgnoreCase("E0000")) {
                onSetConfigFailed();
            } else {
                onSuccessLayout();
            }
        }
    }

    private void onSuccessLayout() {
        switch (whichCheck) {
            case CHECK_APP:
                tv_tipNotifyApp.setVisibility(isPreSelected ? View.GONE : View.VISIBLE);
                break;
            case CHECK_SMS:
                tv_tipNotifySms.setVisibility(isPreSelected ? View.GONE : View.VISIBLE);
                break;
        }
    }

    @Override
    public Activity onActivity() {
        return this;
    }
}

